package com.woniu.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.car.common.ResponseEntity;
import com.woniu.car.domain.TReceiver;
import com.woniu.car.exception.ParamException;
import com.woniu.car.exception.StatisticsException;
import com.woniu.car.mapper.TReceiverMapper;
import com.woniu.car.parameter.ReceiverParam;
import com.woniu.car.parameter.UpdateReceiverParam;
import com.woniu.car.service.TReceiverService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 收获地址表 服务实现类
 * </p>
 *
 * @author zhoubinbin
 * @since 2021-06-21
 */
@Service
public class TReceiverServiceImpl extends ServiceImpl<TReceiverMapper, TReceiver> implements TReceiverService {
    @Resource
    private TReceiverMapper receiverMapper;
    //收获地址新增
    @Override
    public ResponseEntity addReceiver(ReceiverParam receiverParam, Integer userId) {
        TReceiver tReceiver = new TReceiver();
        tReceiver.setUserId(userId);
        BeanUtils.copyProperties(receiverParam,tReceiver);
        int insert = receiverMapper.insert(tReceiver);
        if (insert>0){
            return new ResponseEntity(200,"新增成功",tReceiver);
        }else {
            return new ResponseEntity(400,"新增失败",tReceiver);
        }
    }
    //收获地址删除
    @Override
    public ResponseEntity deleteReceiver(Integer receiverId, Integer userId) throws ParamException {
        if (receiverId==null){
            throw new ParamException("收获地址id不能为空！！！");
        }
        QueryWrapper<TReceiver> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        TReceiver tReceiver = receiverMapper.selectOne(wrapper);
        if (tReceiver==null||tReceiver.getReceiverId()!=receiverId){
            throw new ParamException("该用户没有可删除收获地址");
        }
        int i = receiverMapper.deleteById(receiverId);
        if (i>0){
            return new ResponseEntity(200,"删除成功",tReceiver);
        }
        return new ResponseEntity(400,"删除失败，您要删除的数据不存在",null);
    }

    @Override
    public ResponseEntity selectReceiverAll(Integer userId) {
        QueryWrapper<TReceiver> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<TReceiver> tReceivers = receiverMapper.selectList(wrapper);
        if(tReceivers.size()==0){
            return new ResponseEntity(200,"未查询到该用户信息",null);
        }else {
            return new ResponseEntity(200,"查询成功",tReceivers);
        }
    }
    //收获地址修改
    @Override
    public ResponseEntity updateReceiver(UpdateReceiverParam receiverParam, Integer userId) {
        UpdateWrapper<TReceiver> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("receiver_id",receiverParam.getReceiverId());
        TReceiver tReceiver = new TReceiver();
        BeanUtils.copyProperties(receiverParam,tReceiver);
        int update = receiverMapper.update(tReceiver, wrapper);
        if (update>0){
            return new ResponseEntity(200,"修改成功",tReceiver);
        }else {
            return new ResponseEntity(400,"修改失败",null);
        }
    }
    //设置默认收获地址:需要把该用户其他的收获地址修改不是默认地址
    @Override
    public ResponseEntity addDefaultAddr(Integer receiverId, Integer userId) throws StatisticsException {
        //根据条件查询出当前对象
        if (userId==null){
            throw new StatisticsException("用户id不能为空");
        }

        if (receiverId==null){
            throw new StatisticsException("收获地址id不能为空");
        }
        QueryWrapper<TReceiver> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("receiver_id",receiverId);
        TReceiver tReceiver = receiverMapper.selectById(receiverId);
        if (tReceiver==null){
            throw new StatisticsException("未查询到该地址id所拥有的信息");
        }
        tReceiver.setIsDefault(true);
        List<TReceiver> tReceivers = receiverMapper.selectReceiverByUserId(userId);
            if (!StringUtils.isEmpty(tReceivers)){
                for (int i=0;i<tReceivers.size();i++){
                    TReceiver tReceiver1 = tReceivers.get(i);
                    if (tReceiver1.getIsDefault().equals(true)){
                        tReceiver1.setIsDefault(false);
                        receiverMapper.updateById(tReceiver1);
                    }
                }
                int update = receiverMapper.update(tReceiver, wrapper);
                if (update>0){
                    return new ResponseEntity(200,"设置默认地址成功",tReceiver);
                }
            }else {
                return new ResponseEntity(501,"该用户未添加收获地址信息",null);
            }
        return new ResponseEntity().setMsg("该用户未添加收货地址");
    }
}
